﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using DaInterFace;
using DaInterFace.SYN;
using DaInterFace.SYN.E01;

namespace E01
{

    /// <summary>
    /// 同步销售袋装标准
    /// </summary>
    public class A02 : ISYNChildSet
    {
        public void Code(ISYN handler, CodeResult result, SYNRight user, SerialType st, String parameter)
        {
            E01A02P P = (E01A02P)handler.Serial.Deserialize(st, typeof(E01A02P), parameter);

            try
            {
                SqlParameter[] sps = new SqlParameter[11];
                sps[0] = new SqlParameter("@ID", SqlDbType.NVarChar, 50);
                sps[0].Value = P.ID;
                sps[1] = new SqlParameter("@Number", SqlDbType.NVarChar, 50);
                sps[1].Value = P.Number;
                sps[2] = new SqlParameter("@Name", SqlDbType.NVarChar, 50);
                sps[2].Value = P.Name;
                sps[3] = new SqlParameter("@PerBagWeight", SqlDbType.Decimal);
                sps[3].Value = P.PerBagWeight;
                sps[4] = new SqlParameter("@AbovePreLimit", SqlDbType.Int);
                sps[4].Value = P.AbovePreLimit;
                sps[5] = new SqlParameter("@AboveNumLimit", SqlDbType.Decimal);
                sps[5].Value = P.AboveNumLimit;
                sps[6] = new SqlParameter("@AboveNumUnit", SqlDbType.Int);
                sps[6].Value = P.AboveNumUnit;
                sps[7] = new SqlParameter("@LowerPreLimit", SqlDbType.Int);
                sps[7].Value = P.LowerPreLimit;
                sps[8] = new SqlParameter("@LowerLimitNum", SqlDbType.Decimal);
                sps[8].Value = P.LowerLimitNum;
                sps[9] = new SqlParameter("@LowerNumUnit", SqlDbType.Int);
                sps[9].Value = P.LowerNumUnit;
                sps[10] = new SqlParameter("@LowerNumPass", SqlDbType.Decimal);
                sps[10].Value = P.LowerNumPass;


                StringBuilder sql = new StringBuilder();
                sql.Append("SET NOCOUNT ON\r\n");
                sql.Append("DECLARE @FID INT\r\n SET @FID=IsNull((SELECT 1 FROM KEW_ProductsBagRule WHERE FNumber=@Number),0)");
                sql.Append("IF @FID>0 BEGIN\r\n");
                sql.Append("  UPDATE KEW_ProductsBagRule SET FName=@Name WHERE FNumber=@Number\r\n");
                sql.Append("  UPDATE KEW_ProductsBagRuleEntry SET FName=@Name,FDecimal2=@PerBagWeight,FComboBox=@AbovePreLimit,FDecimal=@AboveNumLimit,FComboBox1=@AboveNumUnit,FComboBox2=@LowerPreLimit,FDecimal1=@LowerLimitNum,");
                sql.Append("  FComboBox3=@LowerNumUnit,FDecimal3=@LowerNumPass  WHERE FNumber=@Number\r\n");
                sql.Append("  SELECT 'U',@FID\r\n");
                sql.Append("END \r\nELSE BEGIN\r\n");
                sql.Append("  EXEC GetICMaxNum 'KEW_ProductsBagRule',@FID OUTPUT\r\n");
                sql.Append("  INSERT INTO KEW_ProductsBagRule ([FID],[FNumber],[FName],[FParentID],[FLogic],[FDetail],[FDiscontinued],[FLevels],[FFullNumber],[FClassTypeID]) VALUES (@FID,@Number,@Name,0,-1,1,0,1,@Number,236900117)\r\n");
                sql.Append("  INSERT INTO KEW_ProductsBagRuleEntry ([FClassTypeID],[FID],[FIndex],[FNumber],[FName],[FDecimal2],[FComboBox],[FDecimal],[FComboBox1],[FComboBox2],[FDecimal1],[FComboBox3],[FDecimal3],[FText]) ");
                sql.Append("  VALUES (236900117,@FID,0,@Number,@Name,@PerBagWeight,@AbovePreLimit,@AboveNumLimit,@AboveNumUnit,@LowerPreLimit,@LowerLimitNum,@LowerNumUnit,@LowerNumPass,@ID)\r\n");
                sql.Append("  SELECT 'I',@FID\r\n");
                sql.Append("END \r\n");
                sql.Append("SET NOCOUNT OFF\r\n");
                Recordset rs = handler.Data.ExeProcedure(P.BaseNo, sql.ToString(), sps);
                if (rs.Enable)
                {
                    if (rs.Data.Tables[0].Rows[0][0].ToString() == "U")
                    {
                        result.Result = EnumResult.Update;
                        result.Content = rs.Data.Tables[0].Rows[0][1].ToString();
                    }
                    else
                    {
                        result.Result = EnumResult.Insert;
                        result.Content = rs.Data.Tables[0].Rows[0][1].ToString();
                    }
                    result.Message = String.Format("销售袋装标准同步完成:{0},{1}", P.Number, result.Result.ToString());
                }
                else
                {
                    result.Result = EnumResult.None;
                    result.Message = String.Format("销售袋装标准同步失败:{0}", rs.Message);
                }
            }
            catch (Exception ex)
            {
                result.Result = EnumResult.None;
                result.Message = String.Format("销售袋装标准同步失败:{0}", ex.Message);
            }
        }

    }
    /// <summary>
    /// 销售袋装标准
    /// </summary>
    public class E01A02P
    {
        /// <summary>
        /// 账套编号
        /// </summary>
        public String BaseNo;
        /// <summary>
        /// 三方内码
        /// </summary>
        public String ID;
        /// <summary>
        /// 三方编号
        /// </summary>
        public String Number;
        /// <summary>
        /// 三方名称
        /// </summary>
        public String Name;
        /// <summary>
        /// 每袋重量（kg）
        /// </summary>
        public Decimal PerBagWeight;
        /// <summary>
        /// 高出预装限制
        /// </summary>
        public int AbovePreLimit;
        /// <summary>
        /// 高出数值限制
        /// </summary>
        public Decimal AboveNumLimit;
        /// <summary>
        /// 高出数值单位
        /// </summary>
        public int AboveNumUnit;
        /// <summary>
        /// 低于预装限制
        /// </summary>
        public int LowerPreLimit;
        /// <summary>
        /// 低出限制数值
        /// </summary>
        public Decimal LowerLimitNum;
        /// <summary>
        /// 低出数值单位
        /// </summary>
        public int LowerNumUnit;
        /// <summary>
        /// 低于该数值通过（kg）
        /// </summary>
        public Decimal LowerNumPass;

    }
}
